package com.drinks.yiniang.common.camera.album;

import android.text.TextUtils;

import com.drinks.yiniang.entity.photo.Photos;

import java.io.File;
import java.util.ArrayList;

/**
 * 存储的返回图片集
 * @author han
 * @date 2019/12/21
 */
public class Result {
    public static ArrayList<Photos> mPhotos = new ArrayList<>();

    /**
     * @return 0：添加成功 -4：选择结果互斥 -3：文件不存在 -2：超过视频选择数 -1：超过图片选择数
     */
    public static int addPhotos(Photos Photos) {
        final String path = Photos.path;
        if (TextUtils.isEmpty(path)) {
            return -3;
        }
        final File file = new File(path);
        if (!file.exists() || !file.isFile()) {
            return -3;
        }
        if (Setting.selectMutualExclusion && mPhotos.size() > 0) {
            if (Photos.type.contains(Type.VIDEO) != mPhotos.get(0).type.contains(Type.VIDEO)) {
                return -4;
            }
        }
        if (Setting.videoCount != -1 || Setting.pictureCount != -1) {
            int number = getVideoNumber();
            if (Photos.type.contains(Type.VIDEO) && number >= Setting.videoCount) {
                return -2;
            }
            number = mPhotos.size() - number;
            if ((!Photos.type.contains(Type.VIDEO)) && number >= Setting.pictureCount) {
                return -1;
            }
        }
        mPhotos.add(Photos);
        return 0;
    }

    public static void removePhotos(Photos Photos) {
        mPhotos.remove(Photos);
    }

    public static void removePhotos(int PhotosIndex) {
        removePhotos(mPhotos.get(PhotosIndex));
    }

    public static void removeAll() {
        int size = mPhotos.size();
        for (int i = 0; i < size; i++) {
            removePhotos(0);
        }
    }

    private static int getVideoNumber() {
        int count = 0;
        for (Photos p : mPhotos) {
            if (p.type.contains(Type.VIDEO)) {
                count += 1;
            }
        }
        return count;
    }

    public static void processOriginal() {
        if (Setting.showOriginalMenu) {
            if (Setting.originalMenuUsable) {
                for (Photos Photos : mPhotos) {
                    Photos.selectedOriginal = Setting.selectedOriginal;
                }
            }
        }
    }

    public static void clear() {
        mPhotos.clear();
    }

    public static boolean isEmpty() {
        return mPhotos.isEmpty();
    }

    public static int count() {
        return mPhotos.size();
    }

    /**
     * 获取选择器应该显示的数字
     *
     * @param Photos 当前图片
     * @return 选择器应该显示的数字
     */
    public static String getSelectorNumber(Photos Photos) {
        return String.valueOf(mPhotos.indexOf(Photos) + 1);
    }

    public static String getPhotosPath(int position) {
        return mPhotos.get(position).path;
    }

    public static String getPhotosType(int position) {
        return mPhotos.get(position).type;
    }

    public static long getPhotosDuration(int position) {
        return mPhotos.get(position).duration;
    }

    public static boolean isSelected(Photos Photos) {
        for (Photos p : Result.mPhotos) {
            if (p.path != null && p.path.equals(Photos.path)) return true;
        }
        return false;
    }
}
